package com.rock.code.bigdata.alarmanalysis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.rock.code.base.service.impl.BaseServiceImpl;
import com.rock.code.bigdata.alarmanalysis.dao.AlarmAnalysisDao;
import com.rock.code.bigdata.alarmanalysis.service.AlarmAnalysisService;
import com.rock.code.common.pager.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by SJ on 2018/11/7.
 */

@Service
public class alarmAnalysisServiceImpl extends BaseServiceImpl<AlarmAnalysisDao> implements AlarmAnalysisService {

    @Resource
    private AlarmAnalysisDao alarmAnalysisDao;

    //昨天的日期
    public static String yesterdayTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd").format(time);
        return dateTime;
    }

    //昨天的开始时间(0时0分0秒)
    public static String yesterdayStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //昨天的结束日期(0时0分0秒)
    public static String yesterdayTimeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //近一周
    public static String sevenDaysBefore() {
        Calendar c = Calendar.getInstance();//日历
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式工具
        c.add(Calendar.DAY_OF_MONTH, -7);//日期偏移,正数向前,负数向后!
        String datetime = sdf.format(c.getTime());
        System.out.println(datetime);
        return datetime;
    }

    //近一月
    public static String oneMonthBefore() {
        Calendar c = Calendar.getInstance();//日历
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式工具
        c.add(Calendar.MONTH, -1);//日期偏移,正数向前,负数向后!
        String datetime = sdf.format(c.getTime());
        return datetime;
    }

    public static String oneMonthBeforeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -1);//日期偏移,正数向前,负数向后!
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //近三月
    public static String threeMonthBefore() {
        Calendar c = Calendar.getInstance();//日历
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式工具
        c.add(Calendar.MONTH, -3);//日期偏移,正数向前,负数向后!
        String datetime = sdf.format(c.getTime());
        return datetime;
    }

    public static String threeMonthBeforeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -3);//日期偏移,正数向前,负数向后!
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //近半年
    public static String sixMonthBefore() {
        Calendar c = Calendar.getInstance();//日历
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式工具
        c.add(Calendar.MONTH, -6);//日期偏移,正数向前,负数向后!
        String datetime = sdf.format(c.getTime());
        return datetime;
    }

    public static String sixMonthBeforeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -6);//日期偏移,正数向前,负数向后!
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //近一年
    public static String oneYearBefore() {
        Calendar c = Calendar.getInstance();//日历
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式工具
        c.add(Calendar.MONTH, -12);//日期偏移,正数向前,负数向后!
        String datetime = sdf.format(c.getTime());
        return datetime;
    }

    public static String oneYearBeforeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -12);//日期偏移,正数向前,负数向后!
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //近一周的开始时间(0时0分0秒)
    public static String sevenDaysBeforeZero() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, -7);//日期偏移,正数向前,负数向后!
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        return dateTime;
    }

    //上周的开始结束日期
    public static String lastWeekTime() {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastBeginDate = sdf.format(calendar1.getTime());
        String lastEndDate = sdf.format(calendar2.getTime());
        return lastBeginDate + "," + lastEndDate;
    }

    //上个月是几月（yyyy-MM）
    public static String lastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM").format(time);
        return dateTime;
    }

    public static String lastMonthBefore() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -13);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM").format(time);
        return dateTime;
    }

    //上个月第一天(yyyy-mm-dd hh24:mi:ss)
    public static String getLastMonthStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        System.out.println(dateTime);
        return dateTime;
    }

    //上个月最后一天(yyyy-mm-dd hh24:mi:ss)
    public static String getLastMonthEndDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        System.out.println(dateTime);
        return dateTime;
    }

    //获取某年某月的第一天日期
    public static String getStartMonthDate(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        String startMonthDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return startMonthDate;
    }

    //获取某年某月的最后一天日期
    public static String getEndMonthDate(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(year, month - 1, day);
        String endMonthDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return endMonthDate;
    }

    //当前年的开始时间
    public static String getStartYeraDate() {
        Calendar c = Calendar.getInstance();
        String startYeraDate = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            startYeraDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return startYeraDate;
    }

    //当前年的开始时间(0时0分0秒)
    public static String getStartYeraDateZero() {
        Calendar c = Calendar.getInstance();
        String startYeraDate = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            startYeraDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return startYeraDate;
    }

    //当前年的结束时间
    public static String getEndYeraDate() {
        Calendar c = Calendar.getInstance();
        String endYeraDate = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            endYeraDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return endYeraDate;
    }

    //三个月前的昨天
    public static String getThreeMonthsAgoDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.add(Calendar.DATE, -1);
        calendar.add(Calendar.MONTH, -3);//把月份减三个月
        Date starDate = calendar.getTime();//得到时间赋给Data
        String stardtr = formatter.format(starDate);//使用格式化Data
        return stardtr;
    }

    //六个月前的昨天
    public static String getSixMonthsAgoDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.add(Calendar.DATE, -1);
        calendar.add(Calendar.MONTH, -6);//把月份减三个月
        Date starDate = calendar.getTime();//得到时间赋给Data
        String stardtr = formatter.format(starDate);//使用格式化Data
        return stardtr;
    }

    //判断标志，传入参数(yyyy-mm-dd)
    public static HashMap getPamars(String DATE_TYPE) {
        HashMap map = new HashMap();
        if ("showOneWeek".equals(DATE_TYPE)) {
            /*map.put("startDate", sevenDaysBefore());
            map.put("endDate", yesterdayTime());*/
            map.put("startDate", "2018-03-19");
            map.put("endDate", "2018-03-25");
        } else if ("showOneMonth".equals(DATE_TYPE)) {
            /*map.put("startDate", oneMonthBefore());
            map.put("endDate", yesterdayTime());*/
            map.put("startDate", "2018-03-01");
            map.put("endDate", "2018-03-31");
        } else if ("showThreeMonth".equals(DATE_TYPE)) {
           /* map.put("startDate", threeMonthBefore());
            map.put("endDate", yesterdayTime());*/
            map.put("startDate", "2017-12-31");
            map.put("endDate", "2018-03-31");
        } else if ("showSixMonth".equals(DATE_TYPE)) {
            /*map.put("startDate", sixMonthBefore());
            map.put("endDate", yesterdayTime());*/
            map.put("startDate", "2017-09-30");
            map.put("endDate", "2018-03-31");
        } else if ("showOneYear".equals(DATE_TYPE)) {
           /* map.put("startDate", oneYearBefore());
            map.put("endDate", yesterdayTime());*/
            map.put("startDate", "2018-01-01");
            map.put("endDate", "2018-03-31");
        } else {
            /*map.put("yesterdayTime", yesterdayTime());*/
            map.put("yesterdayTime", "2018-01-23");
        }
        return map;
    }

    //判断标志，传入参数(yyyy-mm-dd hh24:mi:ss)
    public static HashMap getHourPamars(String DATE_TYPE) {
        HashMap map = new HashMap();
        if ("showOneWeek".equals(DATE_TYPE)) {
            /*map.put("startDate", sevenDaysBefore());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2018-03-19 00:00:00");
            map.put("endDate", "2018-03-25 23:59:59");
        } else if ("showOneMonth".equals(DATE_TYPE)) {
           /* map.put("startDate", oneMonthBeforeZero());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2018-03-01 00:00:00");
            map.put("endDate", "2018-03-31 23:59:59");
        } else if ("showThreeMonth".equals(DATE_TYPE)) {
            /*map.put("startDate", threeMonthBeforeZero());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2017-12-31 00:00:00");
            map.put("endDate", "2018-03-31 23:59:59");
        } else if ("showSixMonth".equals(DATE_TYPE)) {
            /*map.put("startDate", sixMonthBeforeZero());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2017-09-30 00:00:00");
            map.put("endDate", "2018-03-31 23:59:59");
        } else if ("showOneYear".equals(DATE_TYPE)) {
            /*map.put("startDate", oneYearBeforeZero());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2018-01-01 00:00:00");
            map.put("endDate", "2018-03-31 23:59:59");
        } else {
           /* map.put("startDate", yesterdayStartTime());
            map.put("endDate", yesterdayTimeZero());*/
            map.put("startDate", "2018-01-23 00:00:00");
            map.put("endDate", "2018-01-23 23:59:59");
        }
        return map;
    }

    @Override
    public List queryResourceAlarm(String DATE_TYPE) {
        HashMap map = getPamars(DATE_TYPE);
        return alarmAnalysisDao.queryResourceAlarm(map);
    }

    @Override
    public List queryDetails(String EXT_UNIT_ID, String eventClass, String DATE_TYPE, String ROWNUM, String ROW_ID) {
        HashMap map = getHourPamars(DATE_TYPE);
        if (!"null".equals(eventClass) && !"".equals(eventClass)) {
            map.put("eventClass", eventClass);
        }
        if (!"null".equals(EXT_UNIT_ID) && !"".equals(EXT_UNIT_ID)) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
        }
        map.put("ROWNUM", Integer.parseInt(ROWNUM));
        map.put("ROW_ID", Integer.parseInt(ROW_ID));
        return alarmAnalysisDao.queryDetails(map);
    }

    @Override
    public List queryAlarmClassByResource(String EXT_UNIT_ID, String DATE_TYPE) {
        HashMap map = getHourPamars(DATE_TYPE);
        if (!"null".equals(EXT_UNIT_ID) && !"".equals(EXT_UNIT_ID)) {
            map.put("EXT_UNIT_ID", EXT_UNIT_ID);
        }
        return alarmAnalysisDao.queryAlarmClassByResource(map);
    }

}
